function setAddError(errStr)
{
	errTxtNode = document.createTextNode(errStr);
	addErrDiv = document.getElementById("addErr");
	addErrDiv.replaceChild(errTxtNode, addErrDiv.childNodes[0]);
}

function clearAddError()
{
	setAddError(' ');
}

function addToList(wholeList)
{
	textBox = document.getElementById("inputText");
	movie = textBox.value;
	
	if (wholeList.indexOf(movie) >= 0)
	{
		if(document.getElementsByName(movie).length == 0)
		{
			addMovieToList(movie);
			clearAddError();
			textBox.value = '';
		}
		else
		{
			setAddError('Movie already in the list');
		}
	}
	else
	{
		setAddError('Movie doesn\'t exist');
	}

}

function addMovieToList(movie)
{
	list = document.getElementById("chosen");
	
	elmt = document.createElement('li');
	elmt.setAttribute('name', movie);
	
	txtNode = document.createTextNode(movie + '  ');
	elmt.appendChild(txtNode);
	
	remNode = document.createElement('a');
	remTxtNode = document.createTextNode('remove');
	remNode.appendChild(remTxtNode);

	remNode.setAttribute('href', '#');
	remNode.setAttribute('onclick', 'removeFromList("' + movie + '")');
	elmt.appendChild(remNode);
	
	list.appendChild(elmt);
	
	hiddenInput = document.createElement('input');
	hiddenInput.setAttribute('type', 'hidden');
	hiddenInput.setAttribute('name', 'selectedList');
	hiddenInput.setAttribute('value', movie);
	elmt.appendChild(hiddenInput);
}

function removeFromList(name)
{
	nodeToRemove = document.getElementsByName(name)[0];
	nodeToRemove.parentNode.removeChild(nodeToRemove);
	clearAddError();
}
